#include "Processbar.h"

//V1
//void ProBar()
//{
//    char bar[Length];
//    char* lable = "|/-\\";
//    memset(bar, '\0', sizeof(bar));
//    int len = strlen(lable);
//    int cnt = 0;
//    while(cnt <= 100)
//    {
//        printf("[%-100s], [%3d%%], [%c]\r", bar, cnt, lable[cnt%len]);
//        fflush(stdout);
//        bar[cnt++] = Style;
//        usleep(2000);
//    }
//
//    printf("\n");
//}


//V2
void ProBar(double filesize, double current)
{
    char bar[Length];
    char* lable = "|/-\\";
    memset(bar, '\0', sizeof(bar));
    int len = strlen(lable);
    
    double rate = current*100.0/filesize;
    int cnt = 0;
    int loop_count = (int)rate;
    while(cnt <= loop_count)
    {
        bar[cnt++] = Style;
    }

    printf("[%-100s], [%3.1lf%%], [%c]\r", bar, rate, lable[cnt%len]);
    fflush(stdout);
}

void download(callback_t cb)
{
    double filesize = 100*1024*1024*1.0;
    double current = 0.0;
    double bandwidth = 1024*1024*1.0;
   
    printf("download begin, current: %lf\n", current);
    while(current <= filesize)
    {
        //ProBar(filesize, current);
        cb(filesize, current);
        current += bandwidth;
        usleep(20000);
    }
    
    printf("\ndownload done, filesize: %lf\n", filesize);
}
